package com.ld.wms.business.approve.service.impl;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.ld.wms.business.approve.mapper.MaterialExampleReceiveMapper;
import com.ld.wms.business.approve.model.MaterialExampleReceiveModel;
import com.ld.wms.business.approve.service.MaterialExampleReceiveService;
import com.ld.wms.business.approve.vo.req.MaterialExampleReceiveReqVo;
import com.ld.wms.business.approve.vo.resp.MaterialExampleReceiveRespVo;
import com.ld.wms.business.user.model.UserAccountModel;
import com.ld.wms.business.user.service.UserAccountService;
import com.ld.wms.common.vo.GeneratePageListVo;
import com.ld.wms.utils.JWTUtils;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.List;
import java.util.Map;

/**
 * <p>
 * 试剂领用清单 服务实现类
 * </p>
 *
 * @author wangfeng
 * @since 2025-04-16
 */
@Service
public class MaterialExampleReceiveServiceImpl extends ServiceImpl<MaterialExampleReceiveMapper, MaterialExampleReceiveModel> implements MaterialExampleReceiveService {

    @Autowired
    private MaterialExampleReceiveMapper materialExampleReceiveMapper;

    @Autowired
    private JWTUtils jwtUtils;

    @Autowired
    private UserAccountService userAccountService;

    @Override
    public void add(MaterialExampleReceiveReqVo reqVo) {
        Long currentUserAccountId = jwtUtils.getCurrentUserAccountId();
        MaterialExampleReceiveModel model = new MaterialExampleReceiveModel();
        model.setCreateBy(currentUserAccountId);
        model.setCreateTime(new Date());
        BeanUtils.copyProperties(reqVo, model);
        materialExampleReceiveMapper.insert(model);
    }

    @Override
    public GeneratePageListVo<MaterialExampleReceiveRespVo> getPageList(Integer status, Integer current, Integer size) {

        Long currentUserAccountId = jwtUtils.getCurrentUserAccountId();
        IPage<MaterialExampleReceiveRespVo> iPage = new Page<>(current, size);
        IPage<MaterialExampleReceiveRespVo> iPage1 = materialExampleReceiveMapper.selectPageList(iPage, currentUserAccountId, status);
        List<MaterialExampleReceiveRespVo> records = iPage1.getRecords();
        Map<Long, UserAccountModel> allUserMap = userAccountService.getAllUserMap();
        for (MaterialExampleReceiveRespVo record : records) {
            UserAccountModel userAccountModel = allUserMap.get(record.getUserAccountId());
            record.setUserAccountName(userAccountModel.getAccountName());
        }
        return new GeneratePageListVo<>(iPage1);
    }
}
